<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="50%" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right" style="height:450px;overflow:auto">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="报警单据编号" prop="FBILLLNO">
                        <el-input v-model="form.FBILLLNO" placeholder="报警单据编号" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="单据状态" prop="FSTATUS">
                        <el-select v-model="form.FSTATUS" placeholder="单据状态" style="width: 100%">
                            <el-option v-for="item in docList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
               <el-col :span="12">
                    <el-form-item label="设备内码" prop="FEQUID">
                        <div style="display:flex;width:100%">
                            <el-input v-model="form.FEQUID" readonly style="width:100%" placeholder="设备内码"></el-input>
                            <el-button @click="checkDev" type="primary">选择设备</el-button>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备名称" prop="FEQUNAME">
                        <el-input v-model="form.FEQUNAME" readonly placeholder="设备名称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="报警开始时间">
                        <el-date-picker v-model="form.FBEGINDATE" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择开始时间" @change="timeChange"/>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="报警结束时间">
                        <el-date-picker v-model="form.FENDDATE" :value-format="'YYYY-MM-DD'" style="width:100%" type="date" placeholder="请选择结束时间" @change="timeChange"/>
                    </el-form-item>
                </el-col>
            </el-row>
        <el-row :gutter="1">
            <el-col :span="12">
                    <el-form-item label="报警持续时间">
                        <el-input v-model="form.FLONGDURRATION" readonly style="width:100%"></el-input>
                    </el-form-item>
                </el-col>
        </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
        <!-- <main-content v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></main-content> -->
	</el-dialog>
        <device-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false" draggable></device-list>
</template>

<script>
import DeviceList from '@/views/device/deviceList.vue'
export default {
  components: { DeviceList },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增报警项目',
				edit: '编辑报警项目',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            dialog: {
				save: false,
			},
			//表单数据
			form: {
                id:"",
				FBILLLNO: "",
				FEQUID: "",
				FEQUNAME: "",
                FBEGINDATE:"",
                FENDDATE:"",
                FSTATUS:"",
                FLONGDURRATION:"",
                FDES:"",
                FCOMBLIST:"",
                FALARMDES:"",
                FALARMCONDITION:"",
			},
            //验证规则
            rules: {
            FBILLLNO: [{ required: true, message: "请输入报警单据编号" }],
            },
            depList: [],
            docList:[{id:0,name:"未处理"},{id:1,name:"误报"},{id:2,name:"已处理"},{id:3,name:"已报修"}],
		}
	},
    mounted() {
        // this.getDept()
	},
	methods: {
        //计算持续时间
        timeChange(){
            const endDate = new Date(this.form.FENDDATE)
            const startDate = new Date(this.form.FBEGINDATE)
            const timeDiff = endDate - startDate;
            this.form.FLONGDURRATION = Math.ceil(timeDiff / (1000 * 3600 * 24));
        },
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.jiankong.monitorMonitor.edit.post(this.form);
					} else {
						res = await this.$API.jiankong.monitorMonitor.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
            var res = await this.$API.jiankong.monitorMonitor.detail.post({id:data.id});
			this.form.FBILLLNO = res.data.FBILLLNO
			this.form.FEQUID = res.data.FEQUID
			this.form.FEQUNAME = res.data.FEQUNAME
            this.form.FBEGINDATE = res.data.FBEGINDATE;
            this.form.FENDDATE = res.data.FENDDATE
            this.form.FSTATUS = res.data.FSTATUS
            this.form.FLONGDURRATION = res.data.FLONGDURRATION
            this.form.FDES = res.data.FDES
            this.form.FCOMBLIST = res.data.FCOMBLIST
            this.form.FALARMDES = res.data.FALARMDES
            this.form.FALARMCONDITION = res.data.FALARMCONDITION
		},
        // 选择设备
        checkDev(){
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
        },
        handleSuccess(data) {
            this.form.FEQUID = data[0].code
            this.form.FEQUNAME = data[0].title
        },
        async getDept(e){
            var res = await this.$API.all.getDep.get({is_page:0,org_id:e});
            this.depList = res.data;
        },
	}
}
</script>

<style></style>
